package controllers;

import java.util.List;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.zkoss.zul.Button;
import org.zkoss.zul.Textbox;
import render.AlmacenesRenderer;

public class AlmacenesViewController extends GenericForwardComposer {
    Listbox almacenListBox;
    Button cleanButton;
    Button editarButton;
    Textbox filtroCodigo;
    Textbox filtroId;
    Textbox filtroDescripcion;
    Textbox filtroSector;
    Textbox filtroSucursal;
    Textbox filtroStock;
    
    @Override
    public void doAfterCompose (Component comp) throws Exception {
        super.doAfterCompose(comp);
        
        // set models and render to listbox after composed
        almacenListBox.setModel(getAlmacenesModel());

        almacenListBox.setItemRenderer(new AlmacenesRenderer());
    }

    public ListModel getAlmacenesModel () {
        List almacenes;
        
        EntityManagerFactory emf;
        emf = Persistence.createEntityManagerFactory("ConarpesaWebAppPU");
        EntityManager em = emf.createEntityManager();
        javax.persistence.Query q = em.createQuery("select a from Almacen as a");
        almacenes = q.getResultList();
        return new ListModelList(almacenes);
    }
    
    public ListModel getAlmacenesByCodigo () {
        List almacenes;
        EntityManagerFactory emf;
        emf = Persistence.createEntityManagerFactory("ConarpesaWebAppPU");
        EntityManager em = emf.createEntityManager();
        javax.persistence.Query q = em.createQuery("select a from Almacen as a WHERE a.fcodigo LIKE :codigo AND a.fdescripcion LIKE :descripcion AND a.fidnum LIKE :id AND a.fsector LIKE :sector AND a.fsucursal LIKE :sucursal AND a.ftipostock LIKE :stock ORDER By a.fcodigo ASC");
        q.setParameter("codigo", "%" + filtroCodigo.getValue() + "%");
        q.setParameter("descripcion", "%" + filtroDescripcion.getValue() + "%");
        q.setParameter("id", "%" + filtroId.getValue() + "%");
        q.setParameter("sector", "%" + filtroSector.getValue() + "%");
        q.setParameter("sucursal", "%" + filtroSucursal.getValue() + "%");
        q.setParameter("stock", "%" + filtroStock.getValue() + "%");
        almacenes = q.getResultList();
        return new ListModelList(almacenes);
    }
    
    public ListModel getAlmacenesById () {
        List almacenes;
        EntityManagerFactory emf;
        emf = Persistence.createEntityManagerFactory("ConarpesaWebAppPU");
        EntityManager em = emf.createEntityManager();
        javax.persistence.Query q = em.createQuery("select a from Almacen as a WHERE a.fcodigo LIKE :codigo AND a.fdescripcion LIKE :descripcion AND a.fidnum LIKE :id AND a.fsector LIKE :sector AND a.fsucursal LIKE :sucursal AND a.ftipostock LIKE :stock ORDER By a.fidnum ASC");
        q.setParameter("codigo", "%" + filtroCodigo.getValue() + "%");
        q.setParameter("descripcion", "%" + filtroDescripcion.getValue() + "%");
        q.setParameter("id", "%" + filtroId.getValue() + "%");
        q.setParameter("sector", "%" + filtroSector.getValue() + "%");
        q.setParameter("sucursal", "%" + filtroSucursal.getValue() + "%");
        q.setParameter("stock", "%" + filtroStock.getValue() + "%");
        almacenes = q.getResultList();
        return new ListModelList(almacenes);
    }
    
    public ListModel getAlmacenesByDescripcion () {
        List almacenes;
        EntityManagerFactory emf;
        emf = Persistence.createEntityManagerFactory("ConarpesaWebAppPU");
        EntityManager em = emf.createEntityManager();
        javax.persistence.Query q = em.createQuery("select a from Almacen as a WHERE a.fcodigo LIKE :codigo AND a.fdescripcion LIKE :descripcion AND a.fidnum LIKE :id AND a.fsector LIKE :sector AND a.fsucursal LIKE :sucursal AND a.ftipostock LIKE :stock ORDER By a.fdescripcion ASC");
        q.setParameter("codigo", "%" + filtroCodigo.getValue() + "%");
        q.setParameter("descripcion", "%" + filtroDescripcion.getValue() + "%");
        q.setParameter("id", "%" + filtroId.getValue() + "%");
        q.setParameter("sector", "%" + filtroSector.getValue() + "%");
        q.setParameter("sucursal", "%" + filtroSucursal.getValue() + "%");
        q.setParameter("stock", "%" + filtroStock.getValue() + "%");
        almacenes = q.getResultList();
        return new ListModelList(almacenes);
    }
    
    public ListModel getAlmacenesBySector () {
        List almacenes;
        EntityManagerFactory emf;
        emf = Persistence.createEntityManagerFactory("ConarpesaWebAppPU");
        EntityManager em = emf.createEntityManager();
        javax.persistence.Query q = em.createQuery("select a from Almacen as a WHERE a.fcodigo LIKE :codigo AND a.fdescripcion LIKE :descripcion AND a.fidnum LIKE :id AND a.fsector LIKE :sector AND a.fsucursal LIKE :sucursal AND a.ftipostock LIKE :stock ORDER By a.fsector ASC");
        q.setParameter("codigo", "%" + filtroCodigo.getValue() + "%");
        q.setParameter("descripcion", "%" + filtroDescripcion.getValue() + "%");
        q.setParameter("id", "%" + filtroId.getValue() + "%");
        q.setParameter("sector", "%" + filtroSector.getValue() + "%");
        q.setParameter("sucursal", "%" + filtroSucursal.getValue() + "%");
        q.setParameter("stock", "%" + filtroStock.getValue() + "%");
        almacenes = q.getResultList();
        return new ListModelList(almacenes);
    }
    
    public ListModel getAlmacenesBySucursal () {
        List almacenes;
        EntityManagerFactory emf;
        emf = Persistence.createEntityManagerFactory("ConarpesaWebAppPU");
        EntityManager em = emf.createEntityManager();
        javax.persistence.Query q = em.createQuery("select a from Almacen as a WHERE a.fcodigo LIKE :codigo AND a.fdescripcion LIKE :descripcion AND a.fidnum LIKE :id AND a.fsector LIKE :sector AND a.fsucursal LIKE :sucursal AND a.ftipostock LIKE :stock ORDER By a.fsucursal ASC");
        q.setParameter("codigo", "%" + filtroCodigo.getValue() + "%");
        q.setParameter("descripcion", "%" + filtroDescripcion.getValue() + "%");
        q.setParameter("id", "%" + filtroId.getValue() + "%");
        q.setParameter("sector", "%" + filtroSector.getValue() + "%");
        q.setParameter("sucursal", "%" + filtroSucursal.getValue() + "%");
        q.setParameter("stock", "%" + filtroStock.getValue() + "%");
        almacenes = q.getResultList();
        return new ListModelList(almacenes);
    }
    
    public ListModel getAlmacenesByStock () {
        List almacenes;
        EntityManagerFactory emf;
        emf = Persistence.createEntityManagerFactory("ConarpesaWebAppPU");
        EntityManager em = emf.createEntityManager();
        javax.persistence.Query q = em.createQuery("select a from Almacen as a WHERE a.fcodigo LIKE :codigo AND a.fdescripcion LIKE :descripcion AND a.fidnum LIKE :id AND a.fsector LIKE :sector AND a.fsucursal LIKE :sucursal AND a.ftipostock LIKE :stock ORDER By a.ftipostock ASC");
        q.setParameter("codigo", "%" + filtroCodigo.getValue() + "%");
        q.setParameter("descripcion", "%" + filtroDescripcion.getValue() + "%");
        q.setParameter("id", "%" + filtroId.getValue() + "%");
        q.setParameter("sector", "%" + filtroSector.getValue() + "%");
        q.setParameter("sucursal", "%" + filtroSucursal.getValue() + "%");
        q.setParameter("stock", "%" + filtroStock.getValue() + "%");
        almacenes = q.getResultList();
        return new ListModelList(almacenes);
    }
    
    public void onChange$filtroCodigo() {
        almacenListBox.setModel(getAlmacenesByCodigo());
        almacenListBox.setItemRenderer(new AlmacenesRenderer());
    }
    
    public void onChange$filtroId() {
        almacenListBox.setModel(getAlmacenesByDescripcion());
        almacenListBox.setItemRenderer(new AlmacenesRenderer());
    }
    
    public void onChange$filtroDescripcion() {
        almacenListBox.setModel(getAlmacenesByDescripcion());
        almacenListBox.setItemRenderer(new AlmacenesRenderer());
    }
    
    public void onChange$filtroSector() {
        almacenListBox.setModel(getAlmacenesBySector());
        almacenListBox.setItemRenderer(new AlmacenesRenderer());
    }
    
    public void onChange$filtroSucursal() {
        almacenListBox.setModel(getAlmacenesBySucursal());
        almacenListBox.setItemRenderer(new AlmacenesRenderer());
    }
    
    public void onChange$filtroStock() {
        almacenListBox.setModel(getAlmacenesByStock());
        almacenListBox.setItemRenderer(new AlmacenesRenderer());
    }
    
    public void onClick$cleanButton() {
        filtroCodigo.setValue("");
        filtroDescripcion.setValue("");
        filtroId.setValue("");
        filtroSector.setValue("");
        filtroSucursal.setValue("");
        filtroStock.setValue("");
        almacenListBox.setModel(getAlmacenesModel());
        almacenListBox.setItemRenderer(new AlmacenesRenderer());
    }
}